package cn.edu.scau.cmi.lijiongtao.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Login extends GridPane {
	// 声明连接对象
	private Connection connection = null;
	// 驱动程序名
	private String driver = "com.mysql.jdbc.Driver";
	// URL
	private String url = "jdbc:mysql://localhost:3306/program6";
	// User
	private String user = "root";
	private String password = "ljt123456";
	//登录界面控件
	private Label lbUser = new Label("账号：");
	private Label lbPws = new Label("密码：");
	private TextField tfUser = new TextField();
	private PasswordField pfPws = new PasswordField();
	private Button stuButton = new Button("学生登录");
	private Button teaButton = new Button("教师登录");
	private Button adminButton = new Button("管理员登录");
	//登陆人信息
	private String olName = "";
	private String olId = "";
	private String table = "";
	
	public Login() {
		init();
	}

	private boolean identify() {
		String id = tfUser.getText().toString();
		String pwd = pfPws.getText().toString();
		boolean flag = false;
		try {
			Class.forName(driver);
			connection = DriverManager.getConnection(url, user, password);
			
			if (!connection.isClosed()) {
				//System.out.println("connect succeed");
				// 创建statement对象，用来执行SQL语句
				Statement statement = connection.createStatement();
				// SQL
				String sql = "select * from " + table + ";";
				ResultSet rs = statement.executeQuery(sql);
				while (rs.next()) {
					/*
					 * System.out.println(id); System.out.println(rs.getString("id"));
					 * System.out.println(pwd); System.out.println(rs.getString("password"));
					 */
					if (id.equals(rs.getString("id")) && pwd.equals(rs.getString("password"))) {
						//System.out.println("Login Succeed");
						olId = rs.getString("id");
						olName = rs.getString("name");
						flag = true;
						break;
					}
				}
				rs.close();
				connection.close();
			}
		} catch (ClassNotFoundException e2) {
			System.out.println("数据库驱动异常");
		} catch (SQLException e3) {
			e3.printStackTrace();
		}
		return flag; 
	}
	
	private void init() {
		this.addRow(2, lbUser, tfUser);
		this.addRow(4, lbPws, pfPws);
		this.addRow(6, stuButton, teaButton, adminButton);
		stuButton.setOnAction(e -> {
			table = "student";
			if (identify()) {
				Stage stage = new Stage();
				BorderPane borderPane = new BorderPane();
				Student pane = new Student(olId, olName);
				pane.setAlignment(Pos.CENTER);
				pane.setPadding(new Insets(5, 10, 5, 10));
				pane.setHgap(5);
				pane.setVgap(5);
				borderPane.setCenter(pane);
				Scene scene = new Scene(borderPane);
				stage.setScene(scene);
				stage.setHeight(300);
				stage.setWidth(400);
				stage.setTitle("学生界面");
				stage.show();
			} else {
				Alert alert = new Alert(AlertType.ERROR);
				alert.setTitle("错误");
				alert.setHeaderText(null);
				alert.setContentText("用户名或密码错误！");
				alert.showAndWait();
			}
		});

		teaButton.setOnAction(e -> {
			table = "teacher";
			if (identify()) {
				Stage stage = new Stage();
				BorderPane borderPane = new BorderPane();
				Teacher pane = new Teacher(olId, olName);
				pane.setAlignment(Pos.CENTER);
				pane.setPadding(new Insets(5, 10, 5, 10));
				pane.setHgap(5);
				pane.setVgap(5);
				borderPane.setCenter(pane);
				Scene scene = new Scene(borderPane);
				stage.setScene(scene);
				stage.setHeight(400);
				stage.setWidth(500);
				stage.setTitle("教师界面");
				stage.show();
			} else {
				Alert alert = new Alert(AlertType.ERROR);
				alert.setTitle("错误");
				alert.setHeaderText(null);
				alert.setContentText("用户名或密码错误！");
				alert.showAndWait();
			}
		});

		adminButton.setOnAction(e -> {
			table = "admin";
			if (identify()) {
				Stage stage = new Stage();
				BorderPane borderPane = new BorderPane();
				Admin pane = new Admin();
				pane.setAlignment(Pos.CENTER);
				pane.setPadding(new Insets(5, 10, 5, 10));
				pane.setHgap(5);
				pane.setVgap(5);
				borderPane.setCenter(pane);
				Scene scene = new Scene(borderPane);
				stage.setScene(scene);
				stage.setHeight(400);
				stage.setWidth(500);
				stage.setTitle("管理员界面");
				stage.show();
			} else {
				Alert alert = new Alert(AlertType.ERROR);
				alert.setTitle("错误");
				alert.setHeaderText(null);
				alert.setContentText("用户名或密码错误！");
				alert.showAndWait();
			}
		});
	}
}
